<?php
namespace Home\Controller;
use Think\Controller;
class FoodController extends BaseController {
    public function foodlist(){
       $user = M('food');
       $data['telephone']=$_SESSION['telephone'];//获取session数据
       //var_dump($data);
       $name = $_POST['name'];
        
        if($name)
        {
            $where['name']=array('like',"%$name%");
            $res = $user->where($where)->select();
            $this->food=$res;
}
    else{
      // $res = $user->select();
    //总记录数
    $records=$user->count();
    //每页显示的条数
    $pageSize=3;
    //总页数
    // $totalPages=ceil($records/$pageSize);
    //当前的页号
      if(isset($_GET["p"]))
      $page=$_GET["p"];
      else $page=1;
      $this->food=$user->order("fid desc")->page($page,$pageSize)->select();
      $Page=new \Think\Page($records,$pageSize);
      $this->link=$Page->show();

     }

       /* $this->food=$res;*///返回数据给显示页面
      $this->food2=$data;
    	$this->display();
    
    }


public function goodslist(){
    $Page = new Page($totalRows);
  }


public function detail($id){
  $foodModel = M('food');
  $res=$foodModel->where("fid=$id")->find();
  $this->food=$res;
  //点击量
  $hittime=$res['hittime'];
  $data['hittime']=$hittime+1;
  $res4=$foodModel->where("fid=$id")->save($data);
  //最受欢迎
  $res2= $foodModel->limit(4)->order("hittime desc")->select();
  $this->food2=$res2;
  $commentModel=M('comments');
  $res3= $commentModel->limit(4)->order("cid desc")->where("fid=$id")->select();
  $this->food3=$res3;
  //评论部分
  $data['telephone']=$_SESSION['telephone'];//获取session数据
  $this->telephone=$data['telephone'];

  $this->display();
  }



    public function release()
    {
       $user = M('food');
       $data['telephone']=$_SESSION['telephone'];
       $this->food= $data['telephone'];
        $this->display();
    }

 public function dorelease(){
        

    $user=D('food');

    if ($user->create()) {
        
        if ($_FILES['photo']['error']==0) {
            $logoinfo = $this->myupload();
            $pname = $logoinfo['photo']['savename'];
              
        }

        else{
            $pname="default.jpg";
        } 
       

        $image = new \Think\Image();
        $image->open('./Public/Uploads/foodphoto/'.$pname);
        $image->thumb(150,150)->save('./Public/Uploads/foodphoto/'.$pname);
        
        $user->photo=$pname;
        var_dump($pname);

       
        if (false!==$user->add()) {

           
  
            $this->success('发布成功','../Food/foodlist');
        }
        else
        {
           $this->error('数据错误','../Food/release'); 
        }        
    }

else{
        $this->error($user->getError());
    }
}



    public function myupload(){
        $upload=new \Think\Upload();//实例化ThinkPhp自带上传类
        $upload->maxSize = 3145728;//设置上传附件大小
        $upload->exts = array('jpg','jpeg','png','gif');//设置附件上传类型
        $upload->rootPath = './Public/Uploads/foodphoto/';
        //$upload->savePath='';
        $upload->autoSub =false;
        
        $upload->saveName = time().'_'.mt_rand();
        $info = $upload->upload();  // 上传文件
        
        if (!$info) {
            $this->error($upload->getError());
        }
        else{
            return $info;
        }



            }
  public function addcomment(){

    $user=D('comments');

    if ($user->create()) {
        if (false!==$user->add()) {

           
  
            $this->success('评论成功');
        }
        else
        {
           $this->error('数据错误','../Food/detail'); 
        }        
    }

else{
        $this->error($user->getError());
    }
  }

}